package com.viu.technology.listener;


import com.viu.technology.po.User;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 使用Spring自带的监听功能
 */
@Component
public class UserRegisterSortListener implements SmartApplicationListener {

    /**
     * 该方法返回true&supportsSourceType同样返回true时，才会调用该监听内的onApplicationEvent方法
     * @param aClass
     * @return
     */
    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
        return true;
    }

    @Override
    public boolean supportsSourceType(Class<?> aClass) {

        //只有在UserService内发布的UserRegisterEvent事件时才会执行下面逻辑
        return true;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        //转换事件类型
        UserRegisterEvent userRegisterEvent = (UserRegisterEvent) applicationEvent;
        //获取注册用户对象信息
        User user = userRegisterEvent.getUser();
        System.out.println("顺序2的监听事件-用户："+user.getName()+"，注册成功，发送邮件通知。");

    }

    /**
     * 控制监听器的顺序
     * @return
     */
    @Override
    public int getOrder() {
        return 1;
    }
}
